package com.dragee.modules.work.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.work.dao.CompetitionOrderDao;
import com.dragee.modules.work.entity.CompetitionOrderEntity;
import com.dragee.modules.work.service.ApplyProjectService;
import com.dragee.modules.work.service.ApplyService;
import com.dragee.modules.work.service.CompetitionOrderService;
import com.dragee.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service("competitionOrderService")
public class CompetitionOrderServiceImpl extends ServiceImpl<CompetitionOrderDao, CompetitionOrderEntity> implements CompetitionOrderService {


    @Resource
    private CompetitionOrderDao competitionOrderDao;
    @Autowired
    private ApplyService applyService;
    @Autowired
    private ApplyProjectService applyProjectService;

    private String business = "competitionOrder";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<CompetitionOrderEntity> wrapper =
                WrapperUtil.getWrapper(business, params, CompetitionOrderEntity.class);
        IPage<CompetitionOrderEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        return new PageUtils(page);
    }

    @Override
    public List<String> getFinishOrderApplyProjectIds(String competitionId) {
        List<CompetitionOrderEntity> orders =
                list(new QueryWrapper<CompetitionOrderEntity>().eq("competitionId", competitionId));
        String resultStr = "";
        for (CompetitionOrderEntity order : orders) {
            if (StrUtil.isNotBlank(order.getApplyProjectIds())) {
                resultStr += order.getApplyProjectIds() + ",";
            }
        }
        String[] split = resultStr.split(",");
        List<String> result = Arrays.asList(split);
        return result;
    }
}